package neusoft.yuejian_11.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import neusoft.yuejian_11.bean.RegBean;
import neusoft.yuejian_11.listener.RetrofitListener;
import neusoft.yuejian_11.model.UserModel;
import neusoft.yuejian_11.R;

//注册(已改）
public class RegActivity extends AppCompatActivity implements RetrofitListener {
    private EditText userName;
    private  EditText password1R;
    private  EditText password2R;
    private  EditText userPhone;
    private  EditText userAddress;
    private  EditText userEmail;
    private Button regR;
    private  Button back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);
        initView();
        setListener();
    }
    private  void setListener() {
        regR.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String username = userName.getText().toString();
                String password = password1R.getText().toString();
                String password2 = password2R.getText().toString();
                String mobilenum = userPhone.getText().toString();
                String address = userAddress.getText().toString();
                String email = userEmail.getText().toString();
                if (!password.equals("") && password.equals(password2)) {
                    UserModel userModel = new UserModel();
                    userModel.register(username, password, mobilenum, address, email,RegActivity.this);

                } else {
                    Toast.makeText(RegActivity.this, "密码不一致", Toast.LENGTH_SHORT).show();
                }
            }

        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setResult(RESULT_CANCELED);
                finish();
            }
        });
    }
    private  void initView(){
        userName = (EditText) findViewById(R.id.userName);
        password1R= (EditText) findViewById(R.id.userpassword);
        password2R = (EditText) findViewById(R.id.passwordL);
        userPhone = (EditText) findViewById(R.id.tel);
        userEmail= (EditText) findViewById(R.id.emil);
        userAddress = (EditText) findViewById(R.id.address);
        regR = (Button) findViewById(R.id.regR);
        back = (Button) findViewById(R.id.back_Login);

    }

    @Override
    public void onSuccess(Object o, int flag) {
        RegBean regBean=(RegBean) o;
        if (regBean.getSuccess().equals("0")){
            Toast.makeText(RegActivity.this,"注册失败",Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(RegActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
            Intent data=new Intent();
            data.putExtra("username", String.valueOf(userName));
            setResult(RESULT_OK,data);
            finish();


        }
    }

    @Override
    public void onFail() {

    }

}

